package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.pojo.FollowUser;
import com.ruoyi.pojo.SchoolName;
import com.ruoyi.service.FollowUserService;
import com.ruoyi.service.SchoolNameService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.R;
import com.ruoyi.vo.FollowUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/followuser")
@CrossOrigin
public class FollowUserController {

    @Autowired
    private FollowUserService followUserService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private SchoolNameService schoolNameService;

    @GetMapping("/select")
    public R<List<FollowUserDto>> getFollowUser(@RequestParam int userid){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userid);
        List<FollowUser> list = followUserService.list(wrapper);
        List<FollowUserDto> userDto = new ArrayList<>();
        for (FollowUser user:list) {
            SysUser sysUser = iSysUserService.selectUserById(user.getFollowuserid().longValue());
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userid",user.getFollowuserid());
            SchoolName one = schoolNameService.getOne(queryWrapper);
            FollowUserDto followUserDto = new FollowUserDto();
            followUserDto.setUser(sysUser);
            followUserDto.setUserid(user.getUserid());
            followUserDto.setFollowuserid(user.getFollowuserid());
            if (one != null) {
                followUserDto.setSchoolName(one.getUserSchool());
            }
            followUserDto.setId(user.getId());
            userDto.add(followUserDto);
        }
        return R.success(userDto);
    }


    @GetMapping("/judgefollow")
    public R<FollowUser> judgeFollow(Integer userid,Integer followid){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userid);
        wrapper.eq("followuserid",followid);
        FollowUser one = followUserService.getOne(wrapper);
        return R.success(one);
    }

    @PostMapping("/addfollow")
    public R<String> addFollow(@RequestBody FollowUser followUser){
        int i = followUserService.addFollow(followUser);
        if (i > 0){
            return R.success("关注成功");
        }else
            return R.error("关注失败");
    }

    @DeleteMapping("/removefollow")
    public R<String> removeFollow(@RequestBody FollowUser followUser){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",followUser.getUserid());
        wrapper.eq("followuserid",followUser.getFollowuserid());
        boolean i = followUserService.remove(wrapper);
        if (i){
            return R.success("取消成功");
        }else
            return R.error("取消失败");
    }

    @GetMapping("/selectlist")
    public R<List<FollowUserDto>> seletList(@RequestParam Integer id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("followuserid",id);
        ArrayList<FollowUserDto> followUserDtos = new ArrayList<>();
        List<FollowUser> list = followUserService.list(wrapper);
        ArrayList<FollowUserDto> dtoList = new ArrayList<>();
        for (FollowUser i:list) {
            SysUser sysUser = iSysUserService.selectUserById(i.getUserid().longValue());
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("userid",i.getUserid());
            SchoolName one = schoolNameService.getOne(wrapper1);
            FollowUserDto followUserDto = new FollowUserDto();
            followUserDto.setUser(sysUser);
            followUserDto.setId(i.getId());
            followUserDto.setUserid(i.getUserid());
            followUserDto.setFollowuserid(i.getFollowuserid());
            if (one != null) {
                followUserDto.setSchoolName(one.getUserSchool());
            }
            dtoList.add(followUserDto);
        }
        return R.success(dtoList);
    }
}
